package jdk7;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lenovo on 2018/12/4.
 */
public class Test {

    public static void main(String[] args) throws IOException {
        Test test = new Test();
        /*
        try {
            System.out.println(test.readFile("E:\\python01.py"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        */
        test.manipulateFiles();

    }

    /**
     * jdk 新特性
     */
    public void test1(){
        /**
         * Java7中支持在数字量中间增加'_'作为分隔符，
         * 下划线只能在数字中间，编译时编译器自动删除数字中的下划线
         */
        int num = 123_567;
        System.out.println(num);
    }

    /**
     * 在Java7中的版本，可以使用addSuppressed方法记录被抑制的异常
     * @param filename
     * @throws IOException
     */
    public void read(String filename) throws IOException{
        FileInputStream fis = null;
        IOException readException = null;
        try {
            fis = new FileInputStream(filename);
        } catch (IOException ex) {
            readException = ex;
        }finally {
            try {
                fis.close();
            } catch (IOException ex) {
               if(readException != null){
                   readException.addSuppressed(ex);
               }else{
                   readException = ex;
               }
            }
        }
        if(readException != null)
            throw  readException;
    }

    /**
     * catch 子句可以同时捕获多个异常
     */
    public void test2(){
        try {
            Integer.parseInt("Hello");
        }
        catch (NumberFormatException | NullPointerException e) {  //使用'|'分割，多个类型，一个对象e
            e.printStackTrace();
        }
    }

    /**
     * try-with-resources语句
     * Java7之前需要在finally中关闭socket、文件、数据库连接等资源；
     * Java7中在try语句中申请资源，实现资源的自动释放（资源类必须实现java.lang.AutoCloseable接口，一般的文件、数据库连接等均已实现该接口，close方法将被自动调用）。
     * @param filename
     * @return
     * @throws IOException
     */
    public String readFile(String filename) throws IOException{
        try(BufferedReader reader = new BufferedReader(new FileReader(filename))){
            StringBuilder builder = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null){
                builder.append(line);
                builder.append(String.format("%n"));
            }
            return builder.toString();
        }
    }

    public void manipulateFiles() throws IOException {
        Path newFile = Files.createFile(Paths.get("new.txt").toAbsolutePath());
        System.out.println(newFile.toString());
        List<String> content = new ArrayList<String>();
        content.add("Hello");
        content.add("World");
        Files.write(newFile, content, Charset.forName("UTF-8"));
        Files.size(newFile);
        Path descPath = Files.createFile(Paths.get("new_bak.text").toAbsolutePath());
        Files.copy(newFile, new FileOutputStream(descPath.toFile()));
        //Files.delete(newFile);
    }
}
